﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Services.Interface;
using HanGu.Entity;
using HanGu.Common;

namespace Hangu.WebClient.Controllers.Admin
{
    public class PhilosopherBemaController : AdminBaseController
    {
        private static readonly IPhilosopherBema serviceBema = ServiceFactory.GetService<IPhilosopherBema>();
        private static readonly ILecturerInfo serviceLecturer = ServiceFactory.GetService<ILecturerInfo>();
        private static readonly IDictionary serviceDictionary = ServiceFactory.GetService<IDictionary>();
        //
        // GET: /PhilosopherBema/

        public ActionResult Index()
        {
            int LectcherID = 0;
            if (int.TryParse(Request["LectcherID"], out LectcherID))
            {
                SearchParma.LectcherID = LectcherID;
            }
            SearchParma.ClassType = 0;
            return View(viewList, GetPhilosopherBemaList());
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            SearchParma.ClassType = 0;
            SearchParma.Pagination.RowCount = null;
            return View(viewList, GetPhilosopherBemaList());
        }


        public ActionResult Plan()
        {
           
            SearchParma.ClassType = 2;
            return View(viewList.Replace("index.", "Plan."), GetPhilosopherBemaList());
        }

        [HttpPost]
        public ActionResult Plan(FormCollection collection)
        {
            SearchParma.Lectcher = collection["Lectcher"];
           
            SearchParma.ClassType = 2;
            SearchParma.Pagination.RowCount = null;
            return View(viewList.Replace("index.","Plan."), GetPhilosopherBemaList());
        }
        List<VBemaLecturerInfo> GetPhilosopherBemaList()
        {
            SearchParma.Lectcher = Request["Lecturer"];
            SearchParma.PalayAdress = Request["PalayAdress"];
            List<VBemaLecturerInfo> philosopherBemaList = serviceBema.GetAllAndLecturer(base.SearchParma);
            ViewData["RowCount"] = SearchParma.Pagination.RowCount;
            return philosopherBemaList;
        }

        public ActionResult Create(int id)
        {
            ViewData["BemaCatalog"] = new SelectList(serviceDictionary.GetByGroupName("BemaCatalog"), "GenerateKey", "Value");

            PhilosopherBema philosopherBema = new PhilosopherBema() { GUID = Helper.GetGuid, CreateTime = DateTime.Now, Creator = CurentUser.UserName, UpdateTime = DateTime.Now, Updater = CurentUser.UserName };
            ViewData["Lectchers"] = GetLectchers();
            ViewData["MemberGrade"] = GetMemberGrade();

            if (id > 0)
            {
                philosopherBema = serviceBema.GetByID(id);
                var vModel = serviceBema.GetAndLecturerByID(id);
                if (vModel != null)
                { philosopherBema.Lecturer = vModel.LecturerName; }
            }
            if (!philosopherBema.IsIndex.HasValue)
            { philosopherBema.IsIndex = false; }
            if (string.IsNullOrEmpty(philosopherBema.TitlePic))
            {
                philosopherBema.TitlePic = "/Content/default.jpg";
            }
            return View(viewCreate, philosopherBema);
        }
        SelectList GetLectchers()
        {
            SelectList selects = new SelectList(serviceLecturer.GetAll(), "ID", "Name");
            return selects;
        }

        SelectList GetMemberGrade()
        {
            SelectList selects = new SelectList(serviceDictionary.GetByGroupName("MemberGrade"), "GenerateKey", "Value");
            return selects;
        }
        //
        // POST: /PhilosopherBema/Create

        [HttpPost]
        [ValidateInput(false)]
        public string Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                string oldPlayURL = collection["oldPlayURL"];
                PhilosopherBema philosopherBema = new PhilosopherBema();
                TryUpdateModel(philosopherBema, collection);
                philosopherBema.IsIndex = collection["IsIndex"] != null;
                philosopherBema.IsTop = collection["IsTop"] != null;
                philosopherBema.Catalog = Request["Catalog"];
                if (!string.IsNullOrEmpty(philosopherBema.PlayURL) && philosopherBema.PlayURL.IndexOf("Temp") >= 0)//从临时文件夹移动
                {
                    string newPath = string.Empty;
                    MoveToFile(philosopherBema.GUID, navTabId, out newPath);
                    philosopherBema.PlayURL = newPath;
                    if (!string.IsNullOrEmpty(oldPlayURL) && oldPlayURL != newPath)//删除旧视屏
                    {
                        new UploadFileManage().DeleteFile(oldPlayURL);
                    }
                }
                else
                {
                    philosopherBema.PlayURL = oldPlayURL;
                }
                philosopherBema.LecturerID = Request["Lecturer.id"];

                if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
                {
                    if (Request.Files[0].ContentType.IndexOf("image") >= 0)
                    {
                        UploadFileManage uploadFile = new UploadFileManage(Request.Files[0]);
                        uploadFile.ClassType = navTabId;
                        uploadFile.Save();
                        if (!uploadFile.Error)
                        {
                            //先删除旧图片
                            if (!string.IsNullOrEmpty(philosopherBema.TitlePic))
                            {
                                uploadFile.DeleteFile(philosopherBema.TitlePic);
                            }

                            philosopherBema.TitlePic = uploadFile.FileInfo["filepath"];
                            if (philosopherBema.ID > 0)
                            {
                                serviceBema.Update(philosopherBema);
                            }
                            else
                            {
                                serviceBema.Add(philosopherBema);
                            }
                        }
                        else
                        {
                            ajaxStr = string.Format(ajaxStr, 300, uploadFile.Message, navTabId, "", "");
                        }
                    }
                    else
                    { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.UnusefulImag, navTabId, "", ""); }
                }
                else
                {
                    if (philosopherBema.ID > 0)
                    {
                        serviceBema.Update(philosopherBema);
                    }
                    else
                    {
                        serviceBema.Add(philosopherBema);
                    }

                }
                string isCloseCurrent = string.Empty;
                if (philosopherBema.ClassType != 1)
                {
                    isCloseCurrent = "closeCurrent";
                }
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", isCloseCurrent);

            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }

        //
        // GET: /PhilosopherBema/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /PhilosopherBema/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Recommend()
        {
            PhilosopherBema philosopherBema = new PhilosopherBema();
            //ViewData["Lectchers"] = GetLectchers();
            ViewData["MemberGrade"] = GetMemberGrade();
            //获取精彩推荐
            philosopherBema = serviceBema.GetByClassType(1);
            if (philosopherBema == null)
            {
                philosopherBema = new PhilosopherBema() { ClassType = 1, GUID = Helper.GetGuid, CreateTime = DateTime.Now, Creator = CurentUser.UserName, UpdateTime = DateTime.Now, Updater = CurentUser.UserName };
            }
            else
            {
                var vModel = serviceBema.GetAndLecturerByID(philosopherBema.ID);
                if (vModel != null)
                { philosopherBema.Lecturer = vModel.LecturerName; }
            }
            if (!philosopherBema.IsIndex.HasValue)
            { philosopherBema.IsIndex = false; }
            if (string.IsNullOrEmpty(philosopherBema.TitlePic))
            {
                philosopherBema.TitlePic = "/Content/default.jpg";
            }
            return View(viewCreate.Replace("create.", "Recommend."), philosopherBema);
        }
        public ActionResult PlanCreate(int id)
        {
            ViewData["BemaCatalog"] = new SelectList(serviceDictionary.GetByGroupName("BemaCatalog"), "GenerateKey", "Value");
            PhilosopherBema philosopherBema = new PhilosopherBema();
            //ViewData["Lectchers"] = GetLectchers();
            ViewData["MemberGrade"] = GetMemberGrade();
            //获取精彩推荐
            philosopherBema = serviceBema.GetByID(id);
            if (philosopherBema == null)
            {
                philosopherBema = new PhilosopherBema() { ClassType = 2, GUID = Helper.GetGuid, CreateTime = DateTime.Now, Creator = CurentUser.UserName, UpdateTime = DateTime.Now, Updater = CurentUser.UserName };
            }
            else
            {
                var vModel = serviceBema.GetAndLecturerByID(philosopherBema.ID);
                if (vModel != null)
                { philosopherBema.Lecturer = vModel.LecturerName; }
            }
            if (!philosopherBema.IsIndex.HasValue)
            { philosopherBema.IsIndex = false; }
            if (string.IsNullOrEmpty(philosopherBema.TitlePic))
            {
                philosopherBema.TitlePic = "/Content/default.jpg";
            }
            return View(viewCreate.Replace("create.", "PlanCreate."), philosopherBema);
        }
        
        //
        // GET: /PhilosopherBema/Delete/5
        [HttpPost]
        public string Delete(int id, string guid)
        {
            try
            {
                try
                {
                    FileManageController filemanage = new FileManageController();
                    filemanage.DeleteExecute(guid);
                }
                catch { }
                serviceBema.Delete(id);
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";

        }
    }
}
